package com.tbycoder.tby.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tbycoder.tby.config.AccessToken;
import com.tbycoder.tby.entity.Request.CommonIdRequest;
import com.tbycoder.tby.entity.Request.GetFileByResourceRequest;
import com.tbycoder.tby.entity.Vo.CommonDicVo;
import com.tbycoder.tby.entity.Vo.FileVo;

import com.tbycoder.tby.entity.Vo.ResourceVo;
import com.tbycoder.tby.service.IIposResoucesFileService;
import com.tbycoder.tby.utils.ApiResponse;
import com.tbycoder.tby.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tby
 * @since 2021-04-14
 */
@RestController
@RequestMapping("/tby/ipos-resouces-file")
@CrossOrigin
public class IposResoucesFileController {

    @Autowired
    FileUtil fileUtil;
    @Autowired
    IIposResoucesFileService iposResoucesFileService;

    @AccessToken
    @RequestMapping("/uploadmajorfile")
    @ApiOperation("班导师上传学生工作文件")
    public ApiResponse<CommonDicVo> uploadMajorFile(
            @RequestParam MultipartFile file,
            String resources
    ){

        CommonDicVo commonDicVo = new CommonDicVo();

        FileVo fileVo = null;
        try {
            fileVo = fileUtil.storeFileInDatabase(file, null);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResponse.error(e.getMessage());
        }
        iposResoucesFileService.uploadmajorfile(fileVo,resources);
        commonDicVo.setKey("fileId");
        commonDicVo.setValue(fileVo.getFileId());
        return ApiResponse.success(commonDicVo);
    }

    @PostMapping("/modifymajorfile")
    @ApiOperation("班导师和辅导员和管理员删除学生工作文件")
    public ApiResponse modifyMajorFile(
            @RequestBody CommonIdRequest commonIdRequest
    ){
        iposResoucesFileService.modifymajorfile(commonIdRequest);
        return ApiResponse.success();
    }
    @PostMapping("/getUrlById")
    @ApiOperation("获得文件地址")
    public ApiResponse<CommonDicVo> getUrl(
            @RequestBody CommonIdRequest commonIdRequest
    ) {
        CommonDicVo commonDicVo = new CommonDicVo();
        String url = iposResoucesFileService.getUrl(commonIdRequest);
        commonDicVo.setKey("url");
        commonDicVo.setValue(url);
        return ApiResponse.success(commonDicVo);
    }
    @PostMapping("/getfileByResource")
    @ApiOperation("获得对应资源的文件")
    public ApiResponse<Page<ResourceVo>> getfileByResource(
            @RequestBody GetFileByResourceRequest getFileByResourceRequest
            ){
        Page<ResourceVo> resourceVoPage = new Page<>();
        try{
            resourceVoPage = iposResoucesFileService.getfileByResource(getFileByResourceRequest);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResponse.error(e.getMessage());
        }
        return ApiResponse.success(resourceVoPage);

    }
}
